package com.wangsd.socket;

import com.alibaba.fastjson.JSONObject;
import com.wangsd.utils.HttpClientUtils;
import com.wangsd.utils.SMSUtils;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

public class SocketClient {

    public void start() {
        try {
            Socket socket = new Socket("localhost", 16908);
            System.out.println("=======监控软件打开成功！！=======");
            InputStream inputStream = socket.getInputStream();
            byte[] buf = new byte[200];
            while (inputStream.read(buf) != -1) {
                try {
                    String news = new String(buf, "GBK");
                    if (news.startsWith("+NEW_SMS")) {
                        System.out.println("客户端接收服务端发送信息：" + news);
                        String[] data = news.split("┇");
                        String phone = data[1];
                        String date = data[3];
                        if ("1069337896888".equals(data[2])) {//辽宁农信
                            int start = data[4].indexOf("人民币");
                            if (start != -1) {
                                String amount = data[4].substring(start + 3, data[4].indexOf("元，"));
                                System.out.println("金额：" + amount);
                                send(amount, phone, date);
                            }
                        } else if ("106576696596".equals(data[2]) || "10659057196596".equals(data[2])) {//浙江农信
                            int start = data[4].indexOf("收入");
                            if (start != -1) {
                                String amount = data[4].substring(start + 2, data[4].indexOf("，可用余额"));
                                System.out.println("金额：" + amount);
                                send(amount, phone, date);
                            }
                        } else if ("106930096369".equals(data[2])) {//河北农信
                            int start = data[4].indexOf("来账");
                            if (start != -1) {
                                String amount = data[4].substring(start + 2, data[4].indexOf("元，"));
                                System.out.println("金额：" + amount);
                                send(amount, phone + "00", date);
                            }
                        }
                    }
                } catch (Exception e) {
                    String s = "短信socket客户端运行异常";
                    JSONObject jsonObject = SMSUtils.sendXSXXsmsPlus("17784495260", s, "socket客户端");
                    System.out.println("短信发送结果 ：" + jsonObject.toJSONString());
                    e.printStackTrace();
                }
            }
            inputStream.close();
            socket.close();
        } catch (IOException e) {
            String s = "短信socket客户端连接异常";
            JSONObject jsonObject = SMSUtils.sendXSXXsmsPlus("17784495260", s, "socket客户端");
            System.out.println("短信发送结果 ：" + jsonObject.toJSONString());
            e.printStackTrace();
        }
    }

    private void send(String amount, String phone, String date) {
        Map<String, String> reqMap = new HashMap<>();
        reqMap.put("username", phone);
        reqMap.put("password", phone.substring(5));
        reqMap.put("payDate", date);
        reqMap.put("totalFee", new Double(Double.parseDouble(amount) * 100).longValue() + "");
        HttpClientUtils.post("http://114.55.243.30:8788/api/user/messageBack", reqMap);
    }
}
